home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / object.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  11.0 KB  |  391 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. #ifndef OBJECT_H
  24. #define OBJECT_H
  25.  
  26. #include "buffer.h"
  27. #include "connection.h"
  28. #include "notification.h"
  29.  
  30. #include <assert.h>
  31. #include <map>
  32. #include <list>
  33.  
  34. #include "arts_export.h"
  35.  
  36. /*
  37.  * BC - Status (2002-03-08): Object_base, Object_skel, Object_stub
  38.  *
  39.  * All of them have to be kept binary compatible carefully, due to interaction
  40.  * with generated code. There are d ptrs in _skel and _stub, NOT TO BE USED
  41.  * NORMALLY. Normally, do use _internalData instead, as this is much faster
  42.  * than creating two d objects per MCOP implementation/stub. Handle with care.
  43.  */
  44.  
  45.  
  46. namespace Arts {
  47. /* custom dispatching functions */
  48.  
  49. typedef void (*DispatchFunction)(void *object, Buffer *request, Buffer *result);
  50. typedef void (*OnewayDispatchFunction)(void *object, Buffer *request);
  51. typedef void (*DynamicDispatchFunction)(void *object, long methodID, Buffer *request, Buffer *result);
  52.  
  53. class ScheduleNode;
  54. class Object_skel;
  55. class Object_stub;
  56. class FlowSystem;
  57. class MethodDef;
  58. class ObjectReference;
  59. class WeakReferenceBase;
  60. class Object;
  61. class ObjectManager;
  62. class DynamicSkeletonData;
  63. class DynamicSkeletonBase;
  64.  
  65. class ARTS_EXPORT Object_base : public NotificationClient {
  66. private:
  67.     friend class DynamicRequest;
  68.     friend class ObjectManager;
  69.     bool _deleteOk;                // ensure that "delete" is not called manually
  70.  
  71. protected:
  72.     /**
  73.      * ObjectInternalData contains private data structures for
  74.      *  - Object_base
  75.      *  - Object_stub
  76.      *  - Object_skel
  77.      *
  78.      * This is an optimization over adding each of them private data pointers,
  79.      * which would lead to some more bloat.
  80.      */
  81.     class ObjectInternalData *_internalData;
  82.  
  83.     struct ObjectStreamInfo;
  84.  
  85.     Object_base();
  86.     virtual ~Object_base();
  87.  
  88.     /*
  89.      * internal management for streams
  90.      */
  91.     ScheduleNode *_scheduleNode;
  92.     std::list<ObjectStreamInfo *> _streamList;
  93.  
  94.     virtual Object_skel *_skel();
  95.     virtual Object_stub *_stub();
  96.  
  97.     enum ObjectLocation { objectIsLocal, objectIsRemote };
  98.     virtual ObjectLocation _location() const = 0;
  99.  
  100.     long _objectID;
  101.     Connection *_connection;
  102.     std::string _internalObjectID;    // two objects are "_isEqual" when these match
  103.     long _nextNotifyID;
  104.     long _refCnt;                // reference count
  105.     static long _staticObjectCount;
  106.  
  107.     void _destroy();            // use this instead of delete (takes care of
  108.                                 // properly removing flow system node)
  109. public:
  110.     static unsigned long _IID;    // interface ID
  111.     /**
  112.      * custom messaging: these can be used to send a custom data to other
  113.      * objects. Warning: these are *not* usable for local objects. You may
  114.      * only use these functions if you know that you are talking to a remote
  115.      * object. Use _allocCustomMessage to allocate a message. Put the data
  116.      * you want to send in the Buffer. After that, call _sendCustomMessage.
  117.      * Don't free the buffer - this will happen automatically.
  118.      */
  119.  
  120.     virtual Buffer *_allocCustomMessage(long handlerID);
  121.     virtual void _sendCustomMessage(Buffer *data);
  122.  
  123.     /*
  124.      * generic capabilities, which allow find out what you can do with an
  125.      * object even if you don't know it's interface
  126.      */
  127.     virtual long _lookupMethod(const Arts::MethodDef &) = 0;
  128.     virtual std::string _interfaceName() = 0;
  129.     virtual class InterfaceDef _queryInterface(const std::string& name) = 0;
  130.     virtual class TypeDef _queryType(const std::string& name) = 0;
  131.     virtual class EnumDef _queryEnum(const std::string& name) = 0;
  132.     virtual std::string _toString() = 0;
  133.  
  134.     /*
  135.      * stuff for streaming (put in a seperate interface?)
  136.      */
  137.     virtual void calculateBlock(unsigned long cycles);
  138.     ScheduleNode *_node();
  139.     virtual FlowSystem _flowSystem() = 0;
  140.  
  141.     /*
  142.      * reference counting
  143.      */
  144.     virtual void _release() = 0;
  145.     virtual void _copyRemote() = 0;
  146.     virtual void _useRemote() = 0;
  147.     virtual void _releaseRemote() = 0;
  148.  
  149.     // BC issue: added _cancelCopyRemote here to avoid virtual function
  150.     void _cancelCopyRemote();
  151.  
  152.     void _addWeakReference(WeakReferenceBase *reference);
  153.     void _removeWeakReference(WeakReferenceBase *reference);
  154.  
  155.     inline Object_base *_copy() {
  156.         assert(_refCnt > 0);
  157.         _refCnt++;
  158.         return this;
  159.     }
  160.  
  161.     // Default I/O info
  162.     virtual std::vector<std::string> _defaultPortsIn() const;
  163.     virtual std::vector<std::string> _defaultPortsOut() const;
  164.  
  165.     // cast operation
  166.     virtual void *_cast(unsigned long iid);
  167.     void *_cast(const std::string& interface);
  168.     
  169.     // Run-time type compatibility check
  170.     virtual bool _isCompatibleWith(const std::string& interfacename) = 0;
  171.  
  172.     // Aggregation
  173.     virtual std::string _addChild(Arts::Object child, const std::string& name) = 0;
  174.     virtual bool _removeChild(const std::string& name) = 0;
  175.     virtual Arts::Object _getChild(const std::string& name) = 0;
  176.     virtual std::vector<std::string> * _queryChildren() = 0;
  177.  
  178.     /*
  179.      * when this is true, a fatal communication error has occurred (of course
  180.      * only possible for remote objects) - maybe your returncode is invalid,
  181.      * maybe your last invocation didn't succeed...
  182.      */
  183.     virtual bool _error();
  184.  
  185.     inline static long _objectCount() { return _staticObjectCount; }
  186.     inline long _mkNotifyID() { return _nextNotifyID++; }
  187.  
  188.     // object creation
  189.     static Object_base *_create(const std::string& subClass = "Object");
  190.  
  191.     // comparison
  192.     bool _isEqual(Object_base *object) const;
  193.  
  194.     // static converter (from reference)
  195.     static Object_base *_fromString(const std::string& objectref);
  196.     static Object_base *_fromReference(class ObjectReference ref, bool needcopy);
  197. };
  198.  
  199. /*
  200.  * Dispatching
  201.  */
  202.  
  203. class Buffer;
  204. class MethodDef;
  205.  
  206.  
  207. class Object_skel_private;
  208. class AnyConstRef;
  209. class AttributeDef;
  210.  
  211. class ARTS_EXPORT Object_skel : virtual public Object_base {
  212. private:
  213.     friend class Object_base;
  214.     friend class DynamicSkeletonData;
  215.     friend class DynamicSkeletonBase;
  216.  
  217.     Object_skel_private *_d_skel;// do not use until there is a very big problem
  218.  
  219.     // reference counting - remote object watching
  220.     
  221.     long _remoteSendCount;        // don't kill objects just sent to other server
  222.     bool _remoteSendUpdated;    // timeout if they don't want the object
  223.     std::list<class Connection *> _remoteUsers;    // who is using it?
  224.  
  225. protected:
  226.     void _addMethod(DispatchFunction disp, void *object, const MethodDef& md);
  227.     void _addMethod(OnewayDispatchFunction disp, void *object,
  228.                                                         const MethodDef& md);
  229.     void _addMethod(DynamicDispatchFunction disp, void *object,
  230.                                                         const MethodDef& md);
  231.     void _initStream(const std::string& name, void *ptr, long flags);
  232.  
  233.     /** stuff relative to attribute notifications **/
  234.     bool _initAttribute(const Arts::AttributeDef& attribute);
  235.     static bool _QueryInitStreamFunc(Object_skel *skel,const std::string& name);
  236.     bool _generateSlots(const std::string& name, const std::string& interface);
  237.  
  238.     /** for DynamicSkeleton: **/
  239.     const MethodDef& _dsGetMethodDef(long methodID);
  240.  
  241. protected:
  242.     void _defaultNotify(const Notification& notification);
  243.     void notify(const Notification& notification);
  244.     void _emit_changed(const char *stream, const AnyConstRef& value);
  245.  
  246.     /**
  247.      * custom messaging: this is used to install a custom data handler that
  248.      * can be used to receive non-standard messages
  249.      */
  250.     long _addCustomMessageHandler(OnewayDispatchFunction handler, void *object);
  251.  
  252.     Object_skel *_skel();
  253.     ObjectLocation _location() const;
  254.  
  255. public:
  256.     Object_skel();
  257.     virtual ~Object_skel();
  258.  
  259.     // reference counting connection drop
  260.     void _disconnectRemote(class Connection *connection);
  261.     void _referenceClean();
  262.  
  263.     // synchronous & asynchronous dispatching
  264.     void _dispatch(Buffer *request, Buffer *result,long methodID);
  265.     void _dispatch(Buffer *request, long methodID);
  266.     long _lookupMethod(const MethodDef &);
  267.  
  268.     /*
  269.      * standard interface for every object skeleton
  270.      */
  271.     static std::string _interfaceNameSkel();
  272.     virtual void _buildMethodTable();
  273.  
  274.     /*
  275.      * reference counting
  276.      */
  277.     virtual void _release();
  278.     virtual void _copyRemote();
  279.     virtual void _useRemote();
  280.     virtual void _releaseRemote();
  281.  
  282.     /*
  283.      * streaming
  284.      */
  285.     FlowSystem _flowSystem();
  286.  
  287.     /*
  288.      * to inspect the (remote) object interface
  289.      */
  290.     virtual std::string _interfaceName();
  291.     InterfaceDef _queryInterface(const std::string& name);
  292.     TypeDef _queryType(const std::string& name);
  293.     EnumDef _queryEnum(const std::string& name);
  294.     virtual std::string _toString();
  295.     
  296.     // Run-time type compatibility check
  297.     bool _isCompatibleWith(const std::string& interfacename);
  298.  
  299.     // Aggregation
  300.     std::string _addChild(Arts::Object child, const std::string& name);
  301.     bool _removeChild(const std::string& name);
  302.     Arts::Object _getChild(const std::string& name);
  303.     std::vector<std::string> * _queryChildren();
  304. };
  305.  
  306. class Object_stub_private;
  307.  
  308. class ARTS_EXPORT Object_stub : virtual public Object_base {
  309. private:
  310.     friend class Object_base;
  311.  
  312.     Object_stub_private *_d_stub;// do not use until there is a very big problem
  313.  
  314. protected:
  315.     long _lookupCacheRandom;
  316.  
  317.     Object_stub();
  318.     Object_stub(Connection *connection, long objectID);
  319.     virtual ~Object_stub();
  320.  
  321.     virtual Object_stub *_stub();
  322.     ObjectLocation _location() const;
  323.  
  324.     enum { _lookupMethodCacheSize = 337 };
  325.     static struct methodCacheEntry {
  326.         methodCacheEntry() : obj(NULL),method(NULL),ID(0) {} ;
  327.         Object_stub *obj;
  328.         const char *method;
  329.         long ID;
  330.     } *_lookupMethodCache;
  331.  
  332.     long _lookupMethodFast(const char *method);
  333.     long _lookupMethod(const MethodDef &);
  334.  
  335. public:
  336.     /*
  337.      * custom messaging
  338.      */
  339.  
  340.     Buffer *_allocCustomMessage(long handlerID);
  341.     void _sendCustomMessage(Buffer *data);
  342.  
  343.     /*
  344.      * to inspect the (remote) object interface
  345.      */
  346.     std::string _interfaceName();
  347.     InterfaceDef _queryInterface(const std::string& name);
  348.     TypeDef _queryType(const std::string& name);
  349.     EnumDef _queryEnum(const std::string& name);
  350.     std::string _toString();
  351.  
  352.     /*
  353.      * streaming
  354.      */
  355.     FlowSystem _flowSystem();
  356.  
  357.     /*
  358.      * reference counting
  359.      */
  360.     virtual void _release();
  361.     virtual void _copyRemote();
  362.     virtual void _useRemote();
  363.     virtual void _releaseRemote();
  364.     
  365.     // Run-time type compatibility check
  366.     bool _isCompatibleWith(const std::string& interfacename);
  367.  
  368.     // Aggregation
  369.     std::string _addChild(Arts::Object child, const std::string& name);
  370.     bool _removeChild(const std::string& name);
  371.     Arts::Object _getChild(const std::string& name);
  372.     std::vector<std::string> * _queryChildren();
  373.  
  374.     /*
  375.      * communication error? this is true when your connection to the remote
  376.      * object is lost (e.g. when the remote server crashed) - your return
  377.      * values are then undefined, so check this before relying too much
  378.      * on some invocation
  379.      */
  380.  
  381.     bool _error();
  382.  
  383.     /*
  384.      * global cleanup
  385.      */
  386.     static void _cleanupMethodCache();
  387. };
  388.  
  389. }
  390. #endif
  391.